package com.xdxc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdxc.model.dto.ExamDto;
import com.xdxc.model.dto.ExamsDTO;
import com.xdxc.model.form.ExamForm;
import com.xdxc.model.entity.Exams;
import com.xdxc.response.Result;
import com.xdxc.service.ExamsService;
import com.xdxc.utils.ConvertUtils;
import com.xdxc.utils.RandomIdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/exam-detail")

public class ExamController {

    @Autowired
    private ExamsService examsService;

    @PostMapping
    public Result createExam(@Valid @RequestBody ExamForm examForm) {
        Exams exam = new Exams();
        BeanUtils.copyProperties(examForm,exam);
        exam.setId(new RandomIdGenerator(2,2).generateId());
        examsService.save(exam);
        return Result.success("成功");
    }

    @PutMapping("/{id}")
    public Result updateExam(@PathVariable Long id, @Valid @RequestBody ExamForm examForm) {
        Exams exams = ConvertUtils.convert(examForm, Exams.class);
        exams.setId(id);
        examsService.updateById(exams);
        return Result.success("");
    }

    @DeleteMapping("/{id}")
    public Result deleteExam(@PathVariable Long id) {
        examsService.removeById(id);
        return Result.success("删除成功").setMsg("删除成功");
    }

    @GetMapping("/{id}")
    public Result getExam(@PathVariable Long id) {
        Exams exams = examsService.getById(id);
        return Result.success("ConvertUtils.convert(exam, ExamVO.class)");
    }

    @GetMapping("/page")
    public Result pageExams(@RequestParam(defaultValue = "1") int page, // 页码，默认为1
    @RequestParam(defaultValue = "5") int pageSize // 每页条数，默认为5
    ){
        Page<Exams> examPage = new Page<>(page, pageSize);
        IPage<Exams> pagedExam = examsService.page(examPage);
        List<Exams> exams = pagedExam.getRecords();

        Result result=Result.success(exams);
       result.add("totalPage",pagedExam.getTotal());
       result.add("currentPage",pagedExam.getCurrent());
       result.add("pageSize",pagedExam.getSize());
        return result;
    }


    @GetMapping("/getExams")
    public Result<List<ExamDto>> getExams(
            @RequestParam(defaultValue = "1") int page, // 页码，默认为1
            @RequestParam(defaultValue = "10") int size  // 每页条数，默认为5
    ) {
        List<ExamDto> examDtoList= examsService.returnExamsData(page,size);
        // 返回结果
        return Result.success(examDtoList)  ;
    }
    @GetMapping("/checkExam")
    public Result<Exams> checkExam(@RequestParam Long examId) {
        // 返回结果
        Exams exam= examsService.getOne(new LambdaQueryWrapper<Exams>().eq(Exams::getId,examId));
        exam.setPassword("");
        exam.setPaperId(null);
        return Result.success(exam);
    }





}